package com.pda.yuanjing.pages.config;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.pda.yuanjing.R;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.SharedPreferencesManger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConfigActivity extends BaseActivity<ConfigModel, ConfigView, ConfigPresent> implements ConfigView {
    private String TAG = "ConfigActivity";

    @BindView(R.id.ck)
    CheckBox ck;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_pass)
    EditText loginPass;
    @BindView(R.id.btlogin)
    AppCompatButton btlogin;
    @BindView(R.id.tvRes)
    TextView tvRes;

    Boolean flag = false;
    String cid = "";
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);
        ButterKnife.bind(this);
        tvTitle.setText("网络设置");
        flag = SharedPreferencesManger.getLoacl();
        ck.setChecked(flag);
        ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                flag=isChecked;
                SharedPreferencesManger.setLoacl(flag);
                if (flag) {
                    loginPhone.setVisibility(View.INVISIBLE);
                    loginPass.setVisibility(View.INVISIBLE);
                    btlogin.setVisibility(View.INVISIBLE);
                }else {
                    loginPhone.setVisibility(View.VISIBLE);
                    loginPass.setVisibility(View.VISIBLE);
                    btlogin.setVisibility(View.VISIBLE);
                }
            }
        });
        if (flag) {
            loginPhone.setVisibility(View.INVISIBLE);
            loginPass.setVisibility(View.INVISIBLE);
            btlogin.setVisibility(View.INVISIBLE);
        }
        if (SharedPreferencesManger.getCid().length() > 0) {
            loginPhone.setText(SharedPreferencesManger.getCid());
            loginPass.setText(SharedPreferencesManger.getHostName());
        } else {
            loginPhone.setText("");
            loginPass.setText("");
        }
//        loginPass.setText("bct.joey.cc");
//        loginPhone.setText("9999");
    }

    @Override
    public ConfigModel createModel() {
        return new ConfigModelImpl(this);
    }

    @Override
    public ConfigView createView() {
        return this;
    }

    @Override
    public ConfigPresent createPresenter() {
        return new ConfigPresent();
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvRes.setText(str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.btlogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.btlogin:
                cid = loginPhone.getText().toString();
                if (cid.length() == 0) {
                    showMsg("请填写企业编号");
                    return;
                }
                url = loginPass.getText().toString();
                if (url.length() == 0) {
                    showMsg("请填写服务器地址");
                    return;
                }
                presenter.config(url, cid);
                break;
        }
    }
}
